package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController("AdminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
@Api("分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    /**
     * 分类分页查询
     */
    @ApiOperation("分类分页查询")
    @GetMapping("/page")
    public Result page(int page,int pageSize,String name,Integer type){
        Page<Category> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(name!=null,Category::getName,name);
        lambdaQueryWrapper.eq(type!=null,Category::getType,type);
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,lambdaQueryWrapper);
        return  Result.success(pageInfo);
    }

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation("新增分类")
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(1);
        categoryService.save(category);
        return Result.success("分类添加成功");
    }

    /**
     * 修改分类
     */
    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
        categoryService.updateById(category);
        return Result.success("分类修改成功");
    }

    /**
     * 根据id删除分类
     */
    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result deleteById(Long id){
        //如果分类关联了菜品则不能删除
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,id).last("limit 1");
        Dish dish =dishService.getOne(lambdaQueryWrapper);
        if(dish!=null){
            return Result.error("当前分类关联了菜品，不能删除");
        }else{
            categoryService.removeById(id);
            return Result.success("删除分类成功");
        }


    }

    /**
     * 禁用、启用套餐
     */
    @ApiOperation("启用、禁用套餐")
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable Integer status,Long id){
        Category category = categoryService.getById(id);
        category.setStatus(status);
        categoryService.updateById(category);
        return Result.success("状态修改成功");
    }

    /**
     * 根据分类类型查询分类名称
     */
    @GetMapping("/list")
    public Result getByTypeId(Integer type){
        LambdaQueryWrapper<Category> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getType,type);
        List<Category> categories = categoryService.list(lambdaQueryWrapper);
        return Result.success(categories);
    }

}
